#include "../tools.h"


class Solution {
public:
    void helper(vector<vector<int>>& res, list<int>& route, int first, const int& n){
        if(first == n){
            vector<int> tmp;
            tmp.assign(route);
            res.push_back(tmp);
            return;
        }

        for(int i = first; i < n; ++i){
            swap(route[first], route[i]);
            helper(res, route, first + 1, n);
            swap(route[first], route[i]);
            while(i + 1 < n && route[i + 1] == route[i]) ++i;
        }
    }

    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<vector<int>> res;
        int size = nums.size();
        if(size == 0) return res;
        if(size == 1) {
            res.push_back(nums);
            return res;
        }

        sort(nums.begin(), nums.end());
        list<int> nums_list;
        nums_list.assign(nums.begin(), nums.end());
        helper(res, nums_list, 0, size);

        return res;
    }
};

int main(int argc, char const *argv[])
{
    Solution sol;

    // vector<int> nums = {1,1,2};
    vector<int> nums = {1, 0, 0, 0, 9};
    auto res = sol.permuteUnique(nums);

    show(res);

    return 0;
}
